// SHA1.groovy

package ycoa.encode

import java.security.MessageDigest

public class SHA1 {
  
  public static String encode(String string) {
    def sha1 = MessageDigest.getInstance("SHA1")
    def digest = sha1.digest((string).getBytes())
    return bytesToString(digest)
  }
  
  private static String bytesToString(byte[] bytes) {
    def string = new StringBuilder()
    for (byte b: bytes) {
      def hexString = Integer.toHexString(0x00FF & b)
      string.append(hexString.length() == 1 ? "0" + hexString : hexString)
    }
    return string.toString()
  }

}

// *EOF*
